Implement VM_BAD_POWER_STATE handling.
authorEwan Mellor <ewan@xensource.com>
Mon, 25 Dec 2006 19:29:05 +0000 (19:29 +0000)
committerEwan Mellor <ewan@xensource.com>
Mon, 25 Dec 2006 19:29:05 +0000 (19:29 +0000)
Signed-off-by: Ewan Mellor <ewan@xensource.com>
tools/python/xen/xend/XendAPI.py
tools/python/xen/xend/XendDomain.py
tools/python/xen/xend/XendError.py
tools/python/xen/xm/messages/en/xen-xm.po

index 1ca52aa5d4480590afcbf083aa1cb6752a9e4f9a..acd8a0e40936907405fde39377504eb8208439ec 100644 (file)
@@ -260,10 +260,14 @@ def do_vm_func(fn_name, vm_ref, *args, **kwargs):
     @param *args: more arguments
     @type *args: tuple
     """
-    xendom = XendDomain.instance()
-    fn = getattr(xendom, fn_name)
-    xendom.do_legacy_api_with_uuid(fn, vm_ref, *args, **kwargs)
-    return xen_api_success_void()
+    try:
+        xendom = XendDomain.instance()
+        fn = getattr(xendom, fn_name)
+        xendom.do_legacy_api_with_uuid(fn, vm_ref, *args, **kwargs)
+        return xen_api_success_void()
+    except VMBadState, exn:
+        return xen_api_error(['VM_BAD_POWER_STATE', vm_ref, exn.expected,
+                              exn.actual])
 
 
 class XendAPI:
index ce6d48776677d6687f919fde4a2671eb6fddb556..d0cb8cfabb1a5b8bd4b66e39ef2c940a20aa0632 100644 (file)
@@ -36,6 +36,7 @@ from xen.xend import XendRoot, XendCheckpoint, XendDomainInfo
 from xen.xend.PrettyPrint import prettyprint
 from xen.xend.XendConfig import XendConfig
 from xen.xend.XendError import XendError, XendInvalidDomain, VmError
+from xen.xend.XendError import VMBadState
 from xen.xend.XendLogging import log
 from xen.xend.XendAPIConstants import XEN_API_VM_POWER_STATE
 from xen.xend.XendConstants import XS_VMROOT
@@ -782,7 +783,9 @@ class XendDomain:
                 raise XendError("Cannot save privileged domain %s" % domname)
 
             if dominfo.state != DOM_STATE_RUNNING:
-                raise XendError("Cannot suspend domain that is not running.")
+                raise VMBadState("Domain is not running",
+                                 POWER_STATE_NAMES[DOM_STATE_RUNNING],
+                                 POWER_STATE_NAMES[dominfo.state])
 
             dom_uuid = dominfo.get_uuid()
 
@@ -932,7 +935,9 @@ class XendDomain:
                 raise XendInvalidDomain(str(domid))
 
             if dominfo.state != DOM_STATE_HALTED:
-                raise XendError("Domain is already running")
+                raise VMBadState("Domain is already running",
+                                 POWER_STATE_NAMES[DOM_STATE_HALTED],
+                                 POWER_STATE_NAMES[dominfo.state])
             
             dominfo.start(is_managed = True)
             self._add_domain(dominfo)
@@ -960,7 +965,9 @@ class XendDomain:
                     raise XendInvalidDomain(str(domid))
 
                 if dominfo.state != DOM_STATE_HALTED:
-                    raise XendError("Domain is still running")
+                    raise VMBadState("Domain is still running",
+                                     POWER_STATE_NAMES[DOM_STATE_HALTED],
+                                     POWER_STATE_NAMES[dominfo.state])
 
                 log.info("Domain %s (%s) deleted." %
                          (dominfo.getName(), dominfo.info.get('uuid')))
index 59471452672e8a1ed6b31936df2e1277127837ee..5033560ea30fe57793d4692f9b91b50fc41f6be2 100644 (file)
@@ -32,6 +32,16 @@ class XendError(Fault):
     def __str__(self):
         return self.value
 
+class VMBadState(XendError):
+    
+    def __init__(self, value, expected, actual):
+        XendError.__init__(self, value)
+        self.expected = expected
+        self.actual = actual
+
+    def __str__(self):
+        return self.value
+
 class VmError(XendError):
     """Vm construction error."""
     pass
index 75d7f5e6fcecab4ba3f0a80cb8e9a0811bbfcb02..28cb03efec2bc1b3267fb1a0fd099b97a4ade436 100644 (file)
@@ -19,7 +19,7 @@
 msgid ""
 msgstr ""
 "Project-Id-Version: Xen-xm 3.0\n"
-"PO-Revision-Date: 2006-12-23 11:04+0000\n"
+"PO-Revision-Date: 2006-12-25 19:24+0000\n"
 "Last-Translator: Ewan Mellor <ewan@xensource.com>\n"
 "Language-Team: xen-devel <xen-devel@lists.xensource.com>\n"
 "MIME-Version: 1.0\n"
@@ -65,3 +65,5 @@ msgstr "The VM handle %(1)s is invalid."
 msgid "VTPM_HANDLE_INVALID"
 msgstr "The VTPM handle %(1)s is invalid."
 
+msgid "VM_BAD_POWER_STATE"
+msgstr "The VM must be %(2)s to perform the requested operation (it is currently %(3)s)."